<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { adminLoginAPI } from '@/api/admin'
import { useUserStore } from '@/stores'
import { encryptAES } from '@/utils/crypto'

const router = useRouter()

// 表单数据（只存储账号，密码不存明文）
const formModel = ref({
  username: '',
  password: ''
})

//获取表单引用
const formRef = ref(null)

// 自定义验证规则
const validateUsername = (rule, value, callback) => {
  if (!value || value.trim() === '') {
    callback(new Error('请输入账号'))
  } else {
    callback()
  }
}

const validatePassword = (rule, value, callback) => {
  if (!value || value.trim() === '') {
    callback(new Error('请输入密码'))
  } else {
    callback()
  }
}

const rules = {
  username: [{ validator: validateUsername, trigger: 'blur' }],
  password: [{ validator: validatePassword, trigger: 'blur' }]
}

//登录函数
const loginAction = async () => {
  // 表单验证
  await formRef.value.validate()

  // 加密密码
  const encryptedPassword = encryptAES(formModel.value.password)

  try {
    const res = await adminLoginAPI({
      username: formModel.value.username,
      password: encryptedPassword
    })
    const { token, userName, id } = res.data.data
    const userStore = useUserStore()
    userStore.setToken(token)
    userStore.setUserInfo({ username: userName, id })
    ElMessage.success('登录成功！')
    router.push('/')
  } catch (error) {
    ElMessage.error(error.msg || '登录失败，返回数据异常')
  }
}
</script>

<template>
  <SkyBackground></SkyBackground>
  <div class="login-container">
    <div class="logo-container">
      <div class="logo">
        <img src="@/assets/images/logo.webp" alt="Logo" />
      </div>

      <span>酱梨管理</span>
    </div>
    <div class="form-container">
      <h2>登录</h2>
      <el-form
        class="login-form"
        ref="formRef"
        :model="formModel"
        :rules="rules"
        label-position="top"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            v-model="formModel.username"
            placeholder="请输入账号"
            :prefix-icon="User"
            size="large"
          ></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            size="large"
            v-model="formModel.password"
            @keydown.enter="loginAction"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="large" @click="loginAction"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="footer-container">
      <div class="beian-info">
        <img
          v-if="import.meta.env.VITE_LOGIN_FOOTER_LOGO"
          :src="import.meta.env.VITE_LOGIN_FOOTER_LOGO"
          alt=""
        />
        <span class="beian-text">豫公网安备41132702000130号</span>
        <a
          v-if="import.meta.env.VITE_LOGIN_FOOTER_LINK"
          :href="import.meta.env.VITE_LOGIN_FOOTER_LINK"
          target="_blank"
          rel="noopener noreferrer"
        >
          豫ICP备2025138023号-1
        </a>
        <span class="beian-divider">|</span>
        <span class="beian-copyright">© 2025 酱梨食刻.</span>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login-container {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  position: relative;
  background: transparent;
  overflow: hidden;
  padding-top: 11vh;
}

.logo-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-bottom: 32px;

  .logo {
    width: 90px;
    height: 90px;
    margin-bottom: 20px;

    img {
      width: 100%;
      height: 100%;
      object-fit: contain;
      filter: drop-shadow(0 2px 8px rgba(0, 0, 0, 0.3));
    }
  }

  span {
    font-size: 2rem;
    font-weight: 600;
    background: linear-gradient(90deg, #012696 0%, #a4e2c6 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    text-shadow: none;
    letter-spacing: 1.5px;
    text-align: center;
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3));
  }
}

.form-container {
  background: rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(12px);
  border-radius: 20px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  padding: 32px 40px;
  max-width: 400px;
  width: 90%;
  display: flex;
  flex-direction: column;
  align-items: center;
  box-sizing: border-box;
  z-index: 1;

  h2 {
    margin-bottom: 24px;
    font-size: 1.6rem;
    color: #a7a7a7;
    font-weight: 600;
    letter-spacing: 1px;
    text-align: center;
  }
}

.login-form {
  width: 100%;

  :deep(.el-form-item) {
    margin-bottom: 18px;
  }

  /* 统一输入框高度，Element Plus 使用 CSS 变量控制 */
  :deep(.el-input) {
    --el-input-height: 40px;

    .el-input__wrapper {
      border-radius: 12px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
      border: 1px solid #e0e0e0;
      transition: all 0.3s ease;

      &:hover {
        border-color: #409eff;
        box-shadow: 0 2px 12px rgba(64, 158, 255, 0.2);
      }

      &.is-focus {
        border-color: #409eff;
        box-shadow: 0 2px 12px rgba(64, 158, 255, 0.3);
      }
    }
  }

  :deep(.el-button) {
    width: 100%;
    height: 40px;
    font-size: 1rem;
    font-weight: 500;
    letter-spacing: 1px;
    border-radius: 12px;
    margin-top: 8px;
    background: linear-gradient(135deg, #409eff 0%, #3a8ee6 100%);
    border: none;
    transition: all 0.3s ease;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 6px 20px rgba(64, 158, 255, 0.4);
    }

    &:active {
      transform: translateY(0);
    }
  }
}

.footer-container {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 20px;
  display: flex;
  justify-content: center;
  padding: 0 16px;
  z-index: 10;
  pointer-events: none;

  .beian-info {
    display: flex;
    align-items: center;
    color: rgba(255, 255, 255, 0.85);
    font-size: 12px;
    line-height: 1.6;
    text-align: center;
    padding: 8px 16px;
    border-radius: 999px;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(8px);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    letter-spacing: 0.2px;
    pointer-events: auto;
    justify-content: center;
    gap: 6px;

    img {
      width: 18px;
      height: auto;
      margin-right: 4px;
      vertical-align: middle;
      display: inline-block;
    }
    .beian-text {
      margin-right: 6px;
    }
    .beian-divider {
      margin: 0 6px;
      color: #bbb;
    }
    a {
      color: rgba(255, 255, 255, 0.9);
      text-decoration: none;
      border-bottom: 1px dashed rgba(255, 255, 255, 0.5);
      transition:
        color 0.2s ease,
        border-color 0.2s ease;
      margin-right: 6px;
      &:hover {
        color: #fff;
        border-color: rgba(255, 255, 255, 0.8);
      }
    }
    .beian-copyright {
      color: #ccc;
    }
  }

  a {
    color: rgba(255, 255, 255, 0.9);
    text-decoration: none;
    border-bottom: 1px dashed rgba(255, 255, 255, 0.5);
    transition:
      color 0.2s ease,
      border-color 0.2s ease;

    &:hover {
      color: #fff;
      border-color: rgba(255, 255, 255, 0.8);
    }
  }
}
</style>
